package com.example.password.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.password.mapper.PwdMapper;
import com.example.password.pojo.Pwd;
import com.example.password.service.PwdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
@Service
public class PwdServiceImpl implements PwdService {
    @Autowired
    PwdMapper pwdMapper;

    @Override
    public int deletePwdById(int id) {
        int result = pwdMapper.deleteById(id);
        return result;
    }

    @Override
    public List<Pwd> getPwd(String accountName) {
        QueryWrapper<Pwd> wrapper = new QueryWrapper<>();
        wrapper.like("account_name",accountName);
        List<Pwd> list = pwdMapper.selectList(wrapper);
        return list;
    }

    @Override
    public List<Pwd> getPwdList() {
        List<Pwd> pwdlist = pwdMapper.selectList(null);
        return pwdlist;
    }

    @Override
    public int addPwd(Pwd pwd) {
        int result = pwdMapper.insert(pwd);
        return result;
    }

    @Override
    public int deletePwd(Pwd pwd) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("account_name",pwd.getAccountName());
        int result = pwdMapper.deleteByMap(map);
        return result;
    }

    @Override
    public int upadtePwd(Pwd pwd) {
        UpdateWrapper<Pwd> wrapper = new UpdateWrapper<>();
        int result = 0;
        if (pwd.getAccountName() != null){
            wrapper.eq("account_name",pwd.getAccountName());
            result = pwdMapper.update(pwd,wrapper);
        }
        return result;
    }
}
